﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Portal.WebControls;

namespace ESF.SharePoint.Utility
{
    class ESFUtility
    {
        internal static string GetResourceString(string resourceFile, string resourceKey, uint language)
        {
            string str2 = SPUtility.GetLocalizedString("$Resources:{ResourceKeyPlaceholder_FileName},{ResourceKeyPlaceholder_KeyName};".Replace("{ResourceKeyPlaceholder_FileName}", resourceFile).Replace("{ResourceKeyPlaceholder_KeyName}", resourceKey), "OsrvCore", language);
            if (str2.StartsWith("$Resources:", StringComparison.OrdinalIgnoreCase))
            {
                return string.Empty;
            }
            return str2;
        }

        //internal static string GetLocalizedString(string element, string resourceFile, uint lcid)
        //{
        //    return SPUtility.GetLocalizedString("$Resources:" + element, resourceFile, lcid);
        //}

        //internal static string GetLocalizedString(string element, string resourceFile)
        //{
        //    return GetLocalizedString("$Resources:" + element, resourceFile, (uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);
        //}

        internal static string GetLocalizedString(string resourceKey)
        {
            uint language = (uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID;
            string str2 = SPUtility.GetLocalizedString("$Resources:" + resourceKey, "ESF.SharePoint.Resources", language);
            if (str2.StartsWith("$Resources:", StringComparison.OrdinalIgnoreCase))
            {
                return string.Empty;
            }
            return str2;
        }

        internal static string GetUserPicture(string picture)
        {
            picture = string.IsNullOrEmpty(picture) ? ResourceFilePath.GetResourceFileUrl(ResourceFileContext.SharedImage, "O14_person_placeHolder_32.png") : GetSmallThumbnailUrl(picture);
            return picture;
        }

        internal static string GetSmallThumbnailUrl(string profileImageUrl)
        {
            return GetThumbnailUrl(profileImageUrl, "_SThumb");
        }

        internal static string GetLargeThumbnailUrl(string profileImageUrl)
        {
            return GetThumbnailUrl(profileImageUrl, "_LThumb");
        }

        private static string GetThumbnailUrl(string profileImageUrl, string strNewSuffix)
        {
            if (System.IO.Path.GetFileNameWithoutExtension(profileImageUrl).EndsWith("_MThumb", StringComparison.Ordinal))
            {
                Uri uri = new Uri(profileImageUrl);
                int num = uri.AbsolutePath.LastIndexOf('/');
                string path = uri.AbsolutePath.Substring(num + 1);
                return (uri.GetLeftPart(UriPartial.Authority) + uri.AbsolutePath.Substring(0, num + 1) + System.IO.Path.GetFileNameWithoutExtension(path).Replace("_MThumb", strNewSuffix) + System.IO.Path.GetExtension(path) + uri.Query);
            }
            return profileImageUrl;
        }


    }
}
